package com.eastfair.imaster.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.annotation.annotation.base.BaseControllerInfo;
import com.eastfair.annotation.base.SubjectTypeEnum;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.cache.redis.RedisOps;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.dynamic.model.BaseVoGroupClass;
import com.eastfair.core.exception.code.ExceptionCode;
import com.eastfair.core.utils.ConvertUtil;
import com.eastfair.echo.core.EchoService;
import com.eastfair.imaster.dto.ExhibitorProductPageQuery;
import com.eastfair.imaster.dto.ExhibitorProductSaveDTO;
import com.eastfair.imaster.dto.ExhibitorProductUpdateDTO;
import com.eastfair.imaster.entity.ExhibitorProduct;
import com.eastfair.imaster.service.ExhibitorProductService;
import com.eastfair.imaster.vo.ExhibitorProductVO;
import com.eastfair.util.PuvUtils;
import com.eastfair.vo.VoProductClass;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 产品表
 * </p>
 *
 * @author liuxiaobai
 * @date 2021-08-24
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/exhibitorProduct")
@Api(value = "ExhibitorProduct", tags = "展品")
public class ExhibitorProductController extends SuperController<ExhibitorProductService, Long, ExhibitorProduct, ExhibitorProductPageQuery, ExhibitorProductSaveDTO, ExhibitorProductUpdateDTO> {

    @Autowired
    private EchoService echoService;
    @Autowired
    private RedisOps redisOps;

    @Override
    public void handlerResult(IPage<ExhibitorProduct> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<ExhibitorProduct> exhibitorProductList = list.stream().map((map) -> {
            ExhibitorProduct exhibitorProduct = ExhibitorProduct.builder().build();
            //TODO 请在这里完成转换
            return exhibitorProduct;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(exhibitorProductList));
    }

    /**
     * 搜索展品
     * @param params 分页参数
     * @return 结果集
     */
    @ApiOperation(value = "搜索展品",notes = "搜索展品")
    @BaseControllerInfo(voGroup = {VoProductClass.SimpleInfo.class, BaseVoGroupClass.all.class})
    @PostMapping("/search")
    public R<Page<ExhibitorProductVO>> search(@RequestBody PageParams<ExhibitorProductPageQuery> params) {
        ExhibitorProductPageQuery query = params.getModel();
        LambdaQueryWrapper<ExhibitorProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ExhibitorProduct::getProjectId, ContextUtil.getProjectId())
                .eq(ExhibitorProduct::getExhibitionId,ContextUtil.getExhibitionId())
                .eq(query.getExhibitorId() != null,ExhibitorProduct::getExhibitorId,query.getExhibitorId());

        Page<ExhibitorProduct> page = this.baseService.page(params.buildPage(),queryWrapper);
        Page<ExhibitorProductVO> voPage = ConvertUtil.convertPage(page, ExhibitorProductVO.class);
        if (page.getRecords().isEmpty()) {
            return success(voPage);
        }
        //todo 拼问卷

        //todo 拼邀约

        //todo 拼收藏

        //todo 拼直播

        voPage.getRecords().parallelStream().forEach(exhibitorProductVo -> {
            //拼pv、uv
            PuvUtils.handlePuv(redisOps,exhibitorProductVo,SubjectTypeEnum.PRODUCT,exhibitorProductVo.getProductId(),0);
        });
        return success(voPage);
    }

    /**
     * 批量新增/修改展品
     * @param exhibitorProducts 展品list
     * @return boolean
     */
    @ApiOperation(value = "批量新增/修改展品",notes = "批量新增/修改展品")
    @PostMapping("/upsert")
    public R<Boolean> upsert(@RequestBody List<ExhibitorProduct> exhibitorProducts) {
        if (exhibitorProducts == null || exhibitorProducts.isEmpty()) {
            return fail(ExceptionCode.ILLEGAL_ARGUMENT_EX);
        }
        this.baseService.upsert(exhibitorProducts);
        return success();
    }


}
